package fr.teuteuf.runstickmanrun.map.block;

import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.physics.box2d.World;

import fr.teuteuf.runstickmanrun.map.MapGenerator;
import fr.teuteuf.runstickmanrun.map.tile.SolidBoxTile;
import fr.teuteuf.runstickmanrun.map.tile.Tile;

public class SuccessiveHolesBlock extends AbstractMapBlock {
	
	public SuccessiveHolesBlock(int difficultyLevel) {
		setDifficulties(1, 2, 5);
		setCurrentLevel(difficultyLevel);
	}

	@Override
	public int generateBlock(World world, MapGenerator mg, int nbHorizontalTiles) {
		int holesNumber = MathUtils.random(1, 10);
		int holeSize;
		int blockLength = 0;
		
		if(getCurrentLevel() >= getMaxDifficultyLevel()) {
			holeSize = MathUtils.random(2, 10);
		} else {
			holeSize = MathUtils.random(2, 8);
		}
		
		mg.addTile(new SolidBoxTile((byte) 3, 2, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, 0 * Tile.TILE_SIZE, world));
		blockLength += 1 + holeSize;
		
		for(int i = 1; i < holesNumber; i++) {
			mg.addTile(new SolidBoxTile((byte) 9, 0, 1, (nbHorizontalTiles + blockLength++) * Tile.TILE_SIZE, 0 * Tile.TILE_SIZE, world));
			if(getCurrentLevel() < getMedDifficultyLevel()) {
				mg.addTile(new SolidBoxTile((byte) 1, 1, 1, (nbHorizontalTiles + blockLength++) * Tile.TILE_SIZE, 0 * Tile.TILE_SIZE, world));
			}
			mg.addTile(new SolidBoxTile((byte) 3, 2, 1, (nbHorizontalTiles + blockLength++) * Tile.TILE_SIZE, 0 * Tile.TILE_SIZE, world));
			blockLength += holeSize;
		}
		
		mg.addTile(new SolidBoxTile((byte) 9, 0, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, 0 * Tile.TILE_SIZE, world));
		blockLength++;
		
		return nbHorizontalTiles + blockLength;
	}

}
